package com.shunda.ysdgj.manager.controller.goods;

import com.shunda.ysdgj.manager.service.goods.GoodsSkuService;
import com.shunda.ysdgj.model.dto.base.IdsDto;
import com.shunda.ysdgj.model.dto.goods.GoodsSkuFindDto;
import com.shunda.ysdgj.model.entity.goods.GoodsSku;
import com.shunda.ysdgj.model.entity.goods.GoodsSpu;
import com.shunda.ysdgj.model.vo.ResultVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Tag(name = "商品Sku接口")
@RestController
@RequestMapping("/api/v1/admin/goods/sku")
@RequiredArgsConstructor
public class GoodsSkuController {

    private final GoodsSkuService goodsSkuService;

    @Operation(summary = "分页获取商品SKU列表")
    @PostMapping("list")
    public ResultVo list(@RequestBody GoodsSkuFindDto goodsSkuFindDto) {
        return ResultVo.success(goodsSkuService.findByPage(goodsSkuFindDto));
    }

    @Operation(summary = "添加商品SKU")
    @PostMapping("add")
    public ResultVo add(HttpServletRequest request, @RequestBody GoodsSku goodsSku) {
        String token = request.getHeader("token");
        if (!goodsSkuService.add(token, goodsSku)) {
            return ResultVo.fail();
        }
        return ResultVo.success();
    }

    @Operation(summary = "修改商品SKU")
    @PostMapping("modify")
    public ResultVo modify(@RequestBody GoodsSku goodsSku) {
        if (!goodsSkuService.modify(goodsSku)) {
            return ResultVo.fail();
        }
        return ResultVo.success();
    }

    @Operation(summary = "商品SKU上架或下架")
    @PostMapping("modifySaleable")
    public ResultVo modifySaleable(@RequestBody GoodsSku goodsSku) {
        if (!goodsSkuService.modifySaleable(goodsSku)) {
            return ResultVo.fail();
        }
        return ResultVo.success();
    }

    @Operation(summary = "商品SKU有效或无效")
    @PostMapping("modifyValid")
    public ResultVo modifyValid(@RequestBody GoodsSku goodsSku) {
        if (!goodsSkuService.modifyValid(goodsSku)) {
            return ResultVo.fail();
        }
        return ResultVo.success();
    }

    @Operation(summary = "删除商品SKU")
    @PostMapping("delete")
    public ResultVo delete(@RequestBody IdsDto idsDto) {
        if (!goodsSkuService.deleteByIds(idsDto)) {
            return ResultVo.fail();
        }
        return ResultVo.success();
    }

}
